﻿Imports System.ComponentModel
Imports System.ComponentModel.Design

<DefaultEvent("RefSelectEvent")> _
Public Class CcapComTypeControl

    Private _pi As CcapDevItem


    Public Sub New(ByVal dtiItem As CcapDevItem)

        ' 此调用是 Windows 窗体设计器所必需的。
        InitializeComponent()
        _pi = dtiItem

        ' 在 InitializeComponent() 调用之后添加任何初始化。
        Init()

    End Sub
    Private Sub Init()

        Me.rb_DevTypeBig.Checked = True
        Me.rb_DevTypeSmall.Checked = False
        Me.tb_Ref1Name.Text = ""
        Me.Nud_RefNum.Value = 0

        _refSelectd = False
    End Sub



    Private Sub getItem()

        If _refItem Is Nothing Then
            _refItem = New CcapCompressorSetItem()
        End If


        _refItem.ProjectID = MainForm.pi.ProjectID
        _refItem.CompressorSetType = IIf(Me.rb_DevTypeBig.Checked, 0, 1)
        _refItem.CompressorCount = Me.Nud_RefNum.Value
        _refItem.CompressorSetName = Me.tb_Ref1Name.Text
        _refItem.devtId = _pi.Id


        _refItem.CompressorGroupList = New CcapCompressorSetItem().CompressorGroupList

        Dim rti As CcapCompressorItem
        For i As Integer = 0 To Me.FlowLayoutPanel1.Controls.Count - 1
            rti = CType(Me.FlowLayoutPanel1.Controls(i), CcapComTypeRef).CompressorItem
            'rti.RefId = i + 1

            _refItem.CompressorGroupList.Add(rti)


        Next


    End Sub
   

    Private Sub SetItem(ByVal value As CcapCompressorSetItem)


        If value.CompressorSetType = 0 Then
            Me.rb_DevTypeBig.Checked = True
        ElseIf value.CompressorSetType = 1 Then
            Me.rb_DevTypeSmall.Checked = True
        Else
            Me.rb_DevTypeBig.Checked = False
            Me.rb_DevTypeSmall.Checked = False
        End If


        Me.Nud_RefNum.Value = value.CompressorCount
        Me.tb_Ref1Name.Text = value.CompressorSetName
        For Each item As CcapCompressorItem In value.CompressorGroupList

            AddRefGroupControl(item)

        Next

    End Sub

  

    Public Sub SetEnable(ByVal b As Boolean)
        Me.Panel1.Enabled = b
        Me.Panel2.Enabled = b
        Me.FlowLayoutPanel1.Enabled = b
    End Sub

 

    Public Sub AddRefGroupControl(ByVal rti As CcapCompressorItem)
        Dim c As New CcapComTypeRef
        c.CompressorItem = rti
        c.refSelected = False
        c.Panel1.Enabled = False
        AddHandler c.ComSelectEvent, AddressOf CompressorRefControl_RefSelectEvent
        Me.FlowLayoutPanel1.Controls.Add(c)


    End Sub
    Private Sub CompressorRefControl_RefSelectEvent(ByVal sender As System.Object, ByVal e As System.EventArgs)
        For Each c As CcapComTypeRef In Me.FlowLayoutPanel1.Controls
            c.refSelected = False
            c.Panel1.Enabled = False
        Next

        CType(sender, CcapComTypeRef).refSelected = True
    End Sub
    Public Function GetSelectedControl() As CcapComTypeRef
        Dim item As CcapComTypeRef = Nothing

        For Each c As CcapComTypeRef In Me.FlowLayoutPanel1.Controls
            If c.refSelected Then
                item = c
                Exit For
            End If
        Next

        Return item
    End Function

    Private Sub CompressorRefControl_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click
        RaiseEvent RefSelectEvent(Me, New EventArgs)
    End Sub

    Public Event RefSelectEvent As EventHandler

    Private _refSelectd As Boolean
    Public Property refSelected() As Boolean
        Get
            Return _refSelectd
        End Get
        Set(ByVal value As Boolean)
            _refSelectd = value
            Me.gbsel.Visible = value
        End Set
    End Property
    Private _refItem As CcapCompressorSetItem
    <Browsable(False)> _
    Public Property refItem() As CcapCompressorSetItem
        Get
            getItem()
            If _refItem Is Nothing Then
                _refItem = New CcapCompressorSetItem()
            End If

            Return _refItem

        End Get
        Set(ByVal value As CcapCompressorSetItem)
            _refItem = value
            SetItem(value)
        End Set
    End Property
End Class